# Figure 1
# =======================================================================================

# keep only observations that were sent out for review
df_tmp <- reviewer_data %>% 
  group_by(manuscript_id) %>% 
  mutate(n_miss_fem =  sum(is.na(r_gender) == TRUE)) %>% 
  ungroup() %>%
  filter(n_miss_fem == 0 & is.na(gender_type3) == FALSE)

ms.list.reviewer <- unique(df_tmp$manuscript_id)

to_plot <- manuscript_data %>% 
  filter(manuscript_id %in% ms.list.reviewer & is.na(reviewership) == F) 

# calculate mean and variation
to_plot <- to_plot %>%
  group_by(gender_type3, reviewership) %>%
  summarise(rev_score_opt_avg = mean(rev_score_opt)
            , non_reject_avg = mean(non_reject)
            , n = n()
            , sd  = sqrt(var(rev_score_opt))
            , se = sd/sqrt(n))

# make factor variables for plotting
to_plot <- to_plot %>% mutate(reviewership = factor(reviewership, levels = c("only male", "mixed", "only female"))
                              , gender_type3 = factor(gender_type3, levels = c("Male", "Mixed Team", "Female"))
)

# calculate sample mean
mean_rev_scrore <- mean(manuscript_data$rev_score_opt, na.rm = T)

# plot
p <- ggplot(to_plot, aes(x = reviewership , y = rev_score_opt_avg, fill = gender_type3)) + 
  geom_col(position = "dodge") + 
  geom_errorbar(aes(x = reviewership, ymin = rev_score_opt_avg-1.96*se, ymax = rev_score_opt_avg+1.96*se)
                , position = position_dodge(width=0.9), width=0.2, colour="black") + 
  scale_fill_viridis_d(option = "D") + 
  labs(x = "Reviewership", y = "Review score", fill = "Authorship") + 
  theme_minimal() + 
  geom_hline(yintercept = mean_rev_scrore, lty = 2) + 
  theme(legend.position = "bottom") +
  geom_text(data = filter(to_plot, gender_type3 == "Female"), aes(label = paste("N=", n, "\n", sep = ""), y = .005)
            , hjust = -1.3, size = 6, col = "black", position = position_dodge(width=0.9)) + 
  geom_text(data = filter(to_plot, gender_type3 == "Male"), aes(label = paste("N=", n, "\n", sep = ""), y = .005)
            , hjust = 2.1, size = 6, col = "white", position = position_dodge(width=0.9)) + 
  geom_text(data = filter(to_plot, gender_type3 == "Mixed Team"), aes(label = paste("N=", n, "\n", sep = ""), y = .005)
            , size = 6, col = "black", position = position_dodge(width=0.9)) + 
  theme(text = element_text(size = 25))

# save

# as PDF
ggsave(plot = p, here("02_output", "02_figures", "figure1.pdf") 
       , width = 14, height = 10, dpi = 300)

# as TIFF 
ggsave(plot = p, here("02_output", "02_figures", "figure1.tiff") 
       , width = 14, height = 10, dpi = 300, device = "tiff")
